﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class GturController : Controller
    {
        //
        // GET: /Gtur/

        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSNhomNgach
                            //where dt.ID == id
                            select dt).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.NhomNgach,
                                 customer.Loai,
                                 customer.HeSoGoc,
                                 customer.HeSoTang,
                                 customer.SoBac,
                                 customer.SoNamTangLuong
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {
            List<BangNhomNgach> nhomngach = NhomNgachModels.GetDSNhomNgach();
            ViewBag.NhomNgach = nhomngach;

            return View();
        }
        public ActionResult Add()
        {

           // ViewBag.Id = id;
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection form)
        {
            BangNhomNgach ng = new BangNhomNgach();
            ng.SoBac = Convert.ToInt32(form["SoBac"]);
            ng.SoNamTangLuong = Convert.ToInt32(form["SoNamTangLuong"]);
            ng.NhomNgach = form["NhomNgach"];
            ng.Loai = form["Loai"];
            ng.HeSoGoc = Convert.ToDouble(form["HeSoGoc"]);
            ng.HeSoTang = Convert.ToDouble(form["HeSoTang"]);

            NhomNgachModels.Add(ng);
            //return RedirectToAction("Index");
            return RedirectToAction("Index");
        }
        public ActionResult Update(int id)
        {
            BangNhomNgach listNG = NhomNgachModels.GetChiTietNhomNgach(id);
            ViewBag.NhomNgach = listNG;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection form, int id)
        {
            BangNhomNgach ng = NhomNgachModels.GetChiTietNhomNgach(id);
            ng.SoBac = Convert.ToInt32(form["SoBac"]);
            
                ng.SoNamTangLuong = Convert.ToInt32(form["SoNamTangLuong"]);
           
            ng.NhomNgach = form["NhomNgach"];
            ng.Loai = form["Loai"];
            ng.HeSoGoc = Convert.ToDouble(form["HeSoGoc"]);
            ng.HeSoTang = Convert.ToDouble(form["HeSoTang"]);


            NhomNgachModels.Update(ng);
            return RedirectToAction("Index");
        }
        public ActionResult Delete(int id)
        {
            BangNhomNgach s = NhomNgachModels.GetChiTietNhomNgach(id);
            NhomNgachModels.Delete(id);
            return RedirectToAction("Index");
        }
        public int GetConfirmDelete(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from cb in context.DSMaNgach
                        where cb.IDNhomNgach == id
                        select cb;
            return query.ToList().Count;
        }
    }
}
